﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OreSystem.BLL;
using OreSystem.Entities;
namespace OreSystemWeb.Areas.Admin.Controllers
{
    public class CarController : Controller
    {
        private CarBLL carBll = new CarBLL();
        private LevelBll levelBll = new LevelBll();
        //
        // GET: /Admin/Car/

        public ActionResult Index(int id)
        {
            ViewData["type"] = id;
            IList<Level> levels = levelBll.GetLevelByLevel(2);
            if (levels.Count <= 0)
            {
                levels.Add(new Level { ID=0, Name="请先添加上层信息" });
            }
            var items = from a in levels select new SelectListItem { Text = a.Name, Value = a.ID.ToString() };
            ViewData["PID"] = items;
            return View();
        }
        public ActionResult GetListByPage(int type,int page=1, int rows=10)
        {
            int total = carBll.GetCarByType(type).Count;
            var lists = carBll.GetListByPage(type,page-1,rows);
            return Json(new {total=total,rows=lists });
        }
        //
        // GET: /Admin/Car/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Admin/Car/Create

        public ActionResult Create(int id)
        {
            return Json(new Car { Type = id, CreateDate=DateTime.Now },JsonRequestBehavior.AllowGet);
        } 

        //
        // POST: /Admin/Car/Create

        [HttpPost]
        public ActionResult Create(Car car)
        {
            try
            {
                // TODO: Add insert logic here
                carBll.Save(car);
                return Json(new {success=true,msg="添加成功" });
            }
            catch(Exception e)
            {
                return Json(new {success=false,msg=e.Message });
            }
        }
        
        //
        // GET: /Admin/Car/Edit/5
 
        public ActionResult Edit(int id)
        {
            return Json(carBll.GetCarById(id),JsonRequestBehavior.AllowGet);
        }

        //
        // POST: /Admin/Car/Edit/5

        [HttpPost]
        public ActionResult Edit(Car car)
        {
            try
            {
                // TODO: Add update logic here
                carBll.Save(car);
               return Json(new {success=true,msg="保存成功" });
            }
            catch
            {
                return Json(new { success = false, msg = "更新失败" });
            }
        }

        //
        // GET: /Admin/Car/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Admin/Car/Delete/5

        [HttpPost]
        public ActionResult Delete(int id,FormCollection from)
        {
            try
            {
                // TODO: Add delete logic here
                carBll.DeleteCar(carBll.GetCarById(id));
                return Json(new { success=true,msg="删除成功" });
            }
            catch(Exception e)
            {
                return Json(new { success=false,msg=e.Message });
            }
        }
        public ActionResult Deletes(IList<int> idlist)
        {
            try
            {
                carBll.Deletes(idlist.Cast<object>().ToList());
                return Json(new { success = true, msg = "删除成功" });
            }
            catch (Exception e)
            {
                return Json(new { success = false, msg = e.Message });
            }
        }
        public ActionResult GetCarsByName(string q,int id)
        {
            int total=0;
           
            var cars = carBll.GetListByKey(q, id, out total);
            
            return Json(new {total=total,rows=cars },JsonRequestBehavior.AllowGet);
        }
    }
}
